package com.example.administrator.pomelostreet.pinpai.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;

import com.example.administrator.pomelostreet.R;
import com.example.administrator.pomelostreet.pinpai.adapter.PinPai_two_RecyclerViewAdapter;
import com.example.administrator.pomelostreet.pinpai.bean.PinPai_TwoBean;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class PinPaiTwoActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private List<PinPai_TwoBean> mList=new ArrayList<>();
    private  String baseURL="http://api.youzibuy.com/tae_item_list?myuid=129829333&tbuid=&device_id=867323021263676&platform=android&v=2.1.0&imei=867323021263676&bundleid=186&mode=0&app_id=7&v_auth=3.Tw3XuxZufEJE1O3YB%2FCFwfeQpjfJqyupPY3ADa6%2BUC0%3D&statinfo=eyJzb3VyY2UiOiItPiIsImNoYW5uZWxpZCI6IjE4NiIsIm9zIjoiMiIsIm90Ijoi5Lit5Zu956e75YqoIiwiYXBuIjoiNCIsInVhIjoiTUkgNExURSIsIm9zdmVyc2lvbiI6IjYuMC4xIiwic2RrdmVyc2lvbiI6MjMsInYiOiIyLjEuMCIsImJ1aWxkdiI6IjIuMS4wIiwidWlkIjoxMjk4MjkzMzMsIm9wZW51ZGlkIjoiOTBlYTZiMzMtNTEzNi00YWRjLTg2MjctMTljZTdhYmM4Mzg3IiwibWFjIjoiODY3MzIzMDIxMjYzNjc2IiwiYW5kcm9pZGlkIjoiMmYxMWIxZGU4MTI2NTJmNyIsImltZWkiOiI4NjczMjMwMjEyNjM2NzYiLCJpbXNpIjoiNDYwMDc1MDEwMzU0OTk4In0=&myclient=0730210018600000&sdkversion=23&activity_id=22&brand_area_id=";
    private  String houURL="&item_id=0&page=1&source=&tab=item&platform=android&v=2.1.0&device_id=867323021263676";
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
              switch (msg.what){
                   case 0:
                       String json = (String) msg.obj;
                       JsonToData(json);

                       PinPai_two_RecyclerViewAdapter adapter=new PinPai_two_RecyclerViewAdapter(PinPaiTwoActivity.this,mList);
                       mRecyclerView.setAdapter(adapter);


                       break;

               }
        }
    };
    private PinPai_TwoBean pinPai_twoBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(getWindow().FEATURE_NO_TITLE);
        setContentView(R.layout.activity_pin_pai_two);

        //跳转的值
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        int url = intent.getIntExtra("url", 222);

        mRecyclerView = (RecyclerView) findViewById(R.id.pinpai_two_recyclerview);
        //瀑布流效果
        StaggeredGridLayoutManager manager=new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(manager);

        loadData(baseURL+url+houURL);




    }


    //网络请求
    private void loadData(final String url) {
        new Thread(){
            @Override
            public void run() {
                super.run();
                OkHttpClient okHttpClient=new OkHttpClient();
                Request request=new Request.Builder().url(url).build();
                try {
                    Response response = okHttpClient.newCall(request).execute();
                    if(response.isSuccessful()){
                        String json = response.body().string();
                        Message msg = Message.obtain();
                        msg.what=0;
                        msg.obj=json;
                        handler.sendMessage(msg);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
    
    //json解析
    public  void JsonToData(String json){
        try {
            JSONArray jsonArray = new JSONObject(json).getJSONArray("items");
            Gson gson=new Gson();
            for (int i = 0; i < jsonArray.length(); i++) {
                TypeToken<PinPai_TwoBean> typeToken=new TypeToken<PinPai_TwoBean>(){};
                JSONObject obj = jsonArray.getJSONObject(i);
                pinPai_twoBean = gson.fromJson(obj.toString(), typeToken.getType());
                mList.add(pinPai_twoBean);

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
}
